package com.nanhai.buddhist.college.module.study.controller.admin.attendancemanagement;

import com.nanhai.buddhist.college.module.collegebase.api.collegeclass.CollegeClassApi;
import com.nanhai.buddhist.college.module.collegebase.api.teacher.TeacherApi;
import com.nanhai.buddhist.college.module.collegebase.api.teacher.dto.TeacherRespDTO;
import com.nanhai.buddhist.college.module.study.controller.teacher.vo.AttendanceManagementCheckReqVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;

import com.nanhai.buddhist.college.framework.excel.core.util.ExcelUtils;

import com.nanhai.buddhist.college.framework.operatelog.core.annotations.OperateLog;
import static com.nanhai.buddhist.college.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.nanhai.buddhist.college.module.study.controller.admin.attendancemanagement.vo.*;
import com.nanhai.buddhist.college.module.study.dal.dataobject.attendancemanagement.AttendanceManagementDO;
import com.nanhai.buddhist.college.module.study.convert.attendancemanagement.AttendanceManagementConvert;
import com.nanhai.buddhist.college.module.study.service.attendancemanagement.AttendanceManagementService;

@Api(tags = "管理后台 - 考勤管理")
@RestController
@RequestMapping("/study/attendance-management")
@Validated
public class AttendanceManagementController {

    @Resource
    private AttendanceManagementService attendanceManagementService;


    @PostMapping("/create")
    @ApiOperation("创建考勤管理")
    @PreAuthorize("@ss.hasPermission('study:attendance-management:create')")
    public CommonResult<String> createAttendanceManagement(@Valid @RequestBody AttendanceManagementCreateReqVO createReqVO) {
        return success(attendanceManagementService.createAttendanceManagement(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新考勤管理")
    @PreAuthorize("@ss.hasPermission('study:attendance-management:update')")
    public CommonResult<Boolean> updateAttendanceManagement(@Valid @RequestBody AttendanceManagementUpdateReqVO updateReqVO) {
        attendanceManagementService.updateAttendanceManagement(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除考勤管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('study:attendance-management:delete')")
    public CommonResult<Boolean> deleteAttendanceManagement(@RequestParam("id") String id) {
        attendanceManagementService.deleteAttendanceManagement(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得考勤管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('study:attendance-management:query')")
    public CommonResult<AttendanceManagementRespVO> getAttendanceManagement(@RequestParam("id") String id) {
        AttendanceManagementDO attendanceManagement = attendanceManagementService.getAttendanceManagement(id);
        return success(AttendanceManagementConvert.INSTANCE.convert(attendanceManagement));
    }

    @GetMapping("/list")
    @ApiOperation("获得考勤管理列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('study:attendance-management:query')")
    public CommonResult<List<AttendanceManagementRespVO>> getAttendanceManagementList(@RequestParam("ids") Collection<String> ids) {
        List<AttendanceManagementDO> list = attendanceManagementService.getAttendanceManagementList(ids);
        return success(AttendanceManagementConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得考勤管理分页")
    @PreAuthorize("@ss.hasPermission('study:attendance-management:query')")
    public CommonResult<PageResult<AttendanceManagementRespVO>> getAttendanceManagementPage(@Valid AttendanceManagementPageReqVO pageVO) {
        PageResult<AttendanceManagementDO> pageResult = attendanceManagementService.getAttendanceManagementPage(pageVO);
        return success(AttendanceManagementConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出考勤管理 Excel")
    @PreAuthorize("@ss.hasPermission('study:attendance-management:export')")
    @OperateLog(type = EXPORT)
    public void exportAttendanceManagementExcel(@Valid AttendanceManagementExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<AttendanceManagementDO> list = attendanceManagementService.getAttendanceManagementList(exportReqVO);
        // 导出 Excel
        List<AttendanceManagementExcelVO> datas = AttendanceManagementConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "考勤管理.xls", "数据", AttendanceManagementExcelVO.class, datas);
    }

    @PutMapping("/check")
    @ApiOperation("审批考勤")
    public CommonResult<Boolean> checkAttendanceLeave(@Valid @RequestBody AttendanceManagementCheckReqVO pageVO) {
        attendanceManagementService.checkAttendanceLeave(pageVO);
        return success(true);
    }




}
